﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YK.ListItemTransfer
{
    public partial class FormMain : Form
    {
        const string ErrorMessage = "'{0}' named control cannot be empty!";
        const string PauseText = "Pause Item Transfer";
        const string ResumeText = "Resume Item Transfer";
        const string ShowText = "S&how";
        const string HideText = "&Hide";

        ItemTransfer itemTransfer;

        public FormMain()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            SetFormForStart();
        }

        private void SetFormForStart()
        {
            buttonPause.Text = PauseText;
            showHideToolStripMenuItem.Text = HideText;
            textBoxSourceSiteCollection.Focus();
        }

        private void Control_Validating(object sender, CancelEventArgs e)
        {
            CheckControl(sender as Control, e);
        }

        private void CheckControl(Control control, CancelEventArgs argument)
        {
            if (control.Text == string.Empty)
            {
                errorProvider1.SetError(control, GetErrorMessage(control));
                argument.Cancel = true;
            }
            else
            {
                errorProvider1.SetError(control, null);
            }
        }

        private string GetErrorMessage(Control control)
        {
            return string.Format(ErrorMessage, control.Name);
        }

        private void buttonStartItemTransfer_Click(object sender, EventArgs e)
        {
            if (ValidateChildren())
                StartProgress();
        }

        private void StartProgress()
        {
            buttonStart.Enabled = false;
            notifyIcon1.ShowBalloonTip(3000, "Sharepoint Item Transfer", "Items are copying now...", ToolTipIcon.Info);
            TransferSetting transferSetting = new TransferSetting();
            transferSetting.SourceSiteCollection = textBoxSourceSiteCollection.Text.Trim();
            transferSetting.SourceSite = textBoxSourceSite.Text.Trim();
            transferSetting.SourceList = textBoxSourceList.Text.Trim();
            transferSetting.DestinationSiteCollection = textBoxDestinationSiteCollection.Text.Trim();
            transferSetting.DestinationSite = textBoxDestinationSite.Text.Trim();
            transferSetting.DestinationList = textBoxDestinationList.Text.Trim();

            itemTransfer = new ItemTransfer(transferSetting, this);
            itemTransfer.Start();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void Cancel()
        {
            itemTransfer.Suspend();

            if (MessageBox.Show("Do you want stop item transfer process?", "Stop process", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                itemTransfer.Stop(true);
            else
                itemTransfer.Resume();
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            Pause();
        }

        private void Pause()
        {
            if (buttonPause.Text == PauseText)
            {
                pauseItemTransferToolStripMenuItem.Text = ResumeText;
                buttonPause.Text = ResumeText;
                itemTransfer.Suspend();
            }
            else
            {
                pauseItemTransferToolStripMenuItem.Text = PauseText;
                buttonPause.Text = PauseText;
                itemTransfer.Resume();
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool IsRunning()
        {
            return itemTransfer != null && itemTransfer.IsRunning;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            New();
        }

        private void New()
        {
            bool clearAll = true;
            if (IsRunning())
            {
                itemTransfer.Suspend();
                if (MessageBox.Show("Do you want stop current process and start new process?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    itemTransfer.Resume();
                    clearAll = false;
                }
                else
                {
                    itemTransfer.Stop(false);
                }
            }

            if (clearAll)
                Clear();
        }

        private void Clear()
        {
            foreach (Control control in panelInput.Controls)
                foreach (Control textBox in control.Controls)
                    if (textBox is TextBox)
                        textBox.Text = string.Empty;

            buttonStart.Enabled = true;
            panelInput.Enabled = true;
            buttonCancel.Enabled = false;
            buttonPause.Enabled = false;
            labelCurrentItem.Text = string.Empty;
            SetFormForStart();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About();
        }

        private static void About()
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();

        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Exit(e);
        }

        private void Exit(FormClosingEventArgs e)
        {
            if (IsRunning())
            {
                itemTransfer.Suspend();
                if (MessageBox.Show("Do you want stop current process and close window?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    e.Cancel = true;
                    itemTransfer.Resume();
                }
                else
                {
                    itemTransfer.Stop(false);
                }
            }
        }

        private void FormMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                HideWindow();
            else
                ShowWindow();
        }

        private void HideWindow()
        {
            this.Hide();
            showHideToolStripMenuItem.Text = ShowText;
            notifyIcon1.ShowBalloonTip(2000, "Sharepoint Item Transfer", "Sharepoint Item Transfer is running...", ToolTipIcon.Info);
        }
        
        private void ShowWindow()
        {
            this.Show();
            showHideToolStripMenuItem.Text = HideText;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            ChangeWindowState();
        }

        private void showHideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangeWindowState();
        }

        private void ChangeWindowState()
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
                this.HideWindow();
            }
        }
    }
}
